package org.sport.logic;

import org.sport.dbInterface.LocOperation;
import org.sport.dbInterface.SpoHisOperation;
import org.sport.init.SportClientSymbol;
import org.sport.init.SportServerSymbol;
import org.sport.model.entityModel.LocationtableEntity;
import org.sport.model.entityModel.SporthistorytableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.model.jsonModel.Json_2_state_plus;
import org.sport.model.jsonModel.Json_2_state_ride;
import org.sport.model.jsonModel.Json_2_state_run;
import org.sport.model.jsonModel.Json_2_state_walk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zehong
 *
 * 主要用于存储运动时候的数据的逻辑
 */
@Service
public class Logic_Sport {

    @Autowired
    SpoHisOperation sportOperation;

    @Autowired
    LocOperation    locOperation;

    @Autowired
    LogicUtils logicUtils;


    //存放步行数据
    public Json_2_state_walk saveWalk(Json_2_state_walk json_2_state_walk)
    {
        System.out.println("进入Logic-saveWalk");

        UsertableEntity usertosearch2=logicUtils.findUser(json_2_state_walk.getPhoneNumber());
        //要进行判空，看user是否找到
        if(usertosearch2== null)
        {
            System.out.println("没找到对应user");
            json_2_state_walk.setResult(SportClientSymbol.Sport.WALK_RESULT_FAIL_USERNOTEXIST);
        }
        else
        {
            System.out.println("找到对应user");
            //填充sport对象
            System.out.println("1");
            SporthistorytableEntity sporthis=new SporthistorytableEntity();
            System.out.println("2");
            String start=json_2_state_walk.getStartTiem();
            sporthis.setStartTiem(Timestamp.valueOf(start));
            System.out.println("3");
            String end=json_2_state_walk.getEndTime();
            sporthis.setEndTime(Timestamp.valueOf(end));
            System.out.println("4");
            sporthis.setSpoDis( Double.parseDouble(String.valueOf(json_2_state_walk.getSpoDis())));
            System.out.println("5");
            sporthis.setSpoType(SportServerSymbol.sporthistorytable.SPOTYPE_WALK);
            System.out.println("6");
            sporthis.setAveSpeed( Double.parseDouble(String.valueOf(json_2_state_walk.getAveSpeed())));
            System.out.println("7");
            sporthis.setCalorie( Double.parseDouble(String.valueOf(json_2_state_walk.getCalorie())));
            System.out.println("8");
            sporthis.setDuration( Double.parseDouble(String.valueOf(json_2_state_walk.getTotTime())));
            System.out.println("9");
            sporthis.setStepNumer(json_2_state_walk.getSteps_count());
            System.out.println("10");
            sporthis.setUsertableByUserId(usertosearch2);
            System.out.println("11");

            boolean addResult=sportOperation.addSpoHisOne(sporthis);
            if (addResult){
                //添加成功
                System.out.println("添加成功");
                json_2_state_walk.setResult(SportClientSymbol.Sport.WALK_RESULT_SUCCESS);
            }
            else {
                //添加失败
                System.out.println("添加失败");
                json_2_state_walk.setResult(SportClientSymbol.Sport.WALK_RESULT_FAIL_DATABASEWRONG);
            }

        }

        return json_2_state_walk;
    }

    //存放跑步数据
    public Json_2_state_run saveRun(Json_2_state_run json_2_state_run)
    {
        System.out.println("进入Logic-saveRun");

        UsertableEntity usertosearch2=logicUtils.findUser(json_2_state_run.getPhoneNumber());
        //要进行判空，看user是否找到
        if(usertosearch2== null)
        {
            System.out.println("没找到对应user");
            json_2_state_run.setResult(SportClientSymbol.Sport.RUN_RESULT_FAIL_USERNOTEXIST);
        }
        else
        {
            System.out.println("找到对应user");
            //填充sport对象
            SporthistorytableEntity sporthis=new SporthistorytableEntity();
            String start=json_2_state_run.getStartTiem();
            sporthis.setStartTiem(Timestamp.valueOf(start));
            System.out.println("3");
            String end=json_2_state_run.getEndTime();
            sporthis.setEndTime(Timestamp.valueOf(end));
            sporthis.setSpoDis( Double.parseDouble(String.valueOf(json_2_state_run.getSpoDis())));
            sporthis.setSpoType(SportServerSymbol.sporthistorytable.SPOTYPE_RUN);
            sporthis.setAveSpeed( Double.parseDouble(String.valueOf(json_2_state_run.getAveSpeed())));
            sporthis.setCalorie( Double.parseDouble(String.valueOf(json_2_state_run.getCalorie())));
            sporthis.setDuration( Double.parseDouble(String.valueOf(json_2_state_run.getTotTime())));
            sporthis.setStepNumer(json_2_state_run.getSteps_count());
            sporthis.setUsertableByUserId(usertosearch2);

            boolean addResult=sportOperation.addSpoHisOne(sporthis);
            if (addResult){
                //添加成功
                System.out.println("添加成功");
                json_2_state_run.setResult(SportClientSymbol.Sport.RUN_RESULT_SUCCESS);
            }
            else {
                //添加失败
                System.out.println("添加失败");
                json_2_state_run.setResult(SportClientSymbol.Sport.RUN_RESULT_FAIL_DATABASEWRONG);
            }

        }

        return json_2_state_run;
    }

    //存放骑行数据
    public Json_2_state_ride saveRide(Json_2_state_ride json_2_state_ride)
    {
        System.out.println("进入Logic-saveRide");

        UsertableEntity usertosearch2=logicUtils.findUser(json_2_state_ride.getPhoneNumber());
        //要进行判空，看user是否找到
        if(usertosearch2== null)
        {
            System.out.println("没找到对应user");
            json_2_state_ride.setResult(SportClientSymbol.Sport.RIDE_RESULT_FAIL_USERNOTEXIST);
        }
        else
        {
            System.out.println("找到对应user");
            //填充sport对象
            SporthistorytableEntity sporthis=new SporthistorytableEntity();
            String start=json_2_state_ride.getStartTiem();
            sporthis.setStartTiem(Timestamp.valueOf(start));
            System.out.println("3");
            String end=json_2_state_ride.getEndTime();
            sporthis.setEndTime(Timestamp.valueOf(end));
            sporthis.setSpoDis( Double.parseDouble(String.valueOf(json_2_state_ride.getSpoDis())));
            sporthis.setSpoType(SportServerSymbol.sporthistorytable.SPOTYPE_RIDE);
            sporthis.setAveSpeed( Double.parseDouble(String.valueOf(json_2_state_ride.getAveSpeed())));
            sporthis.setCalorie( Double.parseDouble(String.valueOf(json_2_state_ride.getCalorie())));
            sporthis.setDuration( Double.parseDouble(String.valueOf(json_2_state_ride.getTotTime())));
            sporthis.setStepNumer(json_2_state_ride.getSteps_count());
            sporthis.setUsertableByUserId(usertosearch2);

            boolean addResult=sportOperation.addSpoHisOne(sporthis);
            if (addResult){
                //添加成功
                System.out.println("添加成功");
                json_2_state_ride.setResult(SportClientSymbol.Sport.RIDE_RESULT_SUCCESS);
            }
            else {
                //添加失败
                System.out.println("添加失败");
                json_2_state_ride.setResult(SportClientSymbol.Sport.RIDE_RESULT_FAIL_DATABASEWRONG);
            }

        }

        return json_2_state_ride;
    }



    //存放骑行数据
    public Json_2_state_plus saveSportPlus(Json_2_state_plus json_2_state_plus)
    {
        System.out.println("进入Logic-savePlus");

        UsertableEntity usertosearch2=logicUtils.findUser(json_2_state_plus.getPhoneNumber());
        //要进行判空，看user是否找到
        if(usertosearch2== null)
        {
            System.out.println("没找到对应user");
            json_2_state_plus.setResult(SportClientSymbol.Sport.RIDE_RESULT_FAIL_USERNOTEXIST);
        }
        else
        {
            SporthistorytableEntity sporthis=new SporthistorytableEntity();
            String start=json_2_state_plus.getStartTiem();
            sporthis.setStartTiem(Timestamp.valueOf(start));
            String end=json_2_state_plus.getEndTime();
            sporthis.setEndTime(Timestamp.valueOf(end));
            sporthis.setSpoDis( Double.parseDouble(String.valueOf(json_2_state_plus.getSpoDis())));
            switch (json_2_state_plus.getSpoType())
            {
                case SportServerSymbol.sporthistorytable.SPOTYPE_RUN:
                    sporthis.setSpoType(SportServerSymbol.sporthistorytable.SPOTYPE_RUN);
                    break;
                case SportServerSymbol.sporthistorytable.SPOTYPE_WALK:
                    sporthis.setSpoType(SportServerSymbol.sporthistorytable.SPOTYPE_WALK);
                    break;
                case SportServerSymbol.sporthistorytable.SPOTYPE_RIDE:
                    sporthis.setSpoType(SportServerSymbol.sporthistorytable.SPOTYPE_RIDE);
                    break;
                default:
                    break;
            }
            sporthis.setAveSpeed( Double.parseDouble(String.valueOf(json_2_state_plus.getAveSpeed())));
            sporthis.setCalorie( Double.parseDouble(String.valueOf(json_2_state_plus.getCalorie())));
            sporthis.setDuration(0.01);
            sporthis.setStepNumer(json_2_state_plus.getSteps_count());
            sporthis.setUsertableByUserId(usertosearch2);
            boolean addResult=sportOperation.addSpoHisOne(sporthis);

            sporthis  =   sportOperation.findOneByUserIdAndStartTime(sporthis);
            System.out.println(sporthis.getId());


            int index = 0;                  //存储轨迹个数
            int stringArrLength =   0;
//            23.035632,114.360903;23.035683000000002,114.360952;23.035733,114.361002;23.035783000000002,114.361052;23.035833,114.361102;23.035883000000002,114.36115199999999;23.035933,114.36120199999999;
            String  locationStringArr[]  =   new String[index];
            locationStringArr   =   json_2_state_plus.getTrack().split(";");
            index   =   locationStringArr.length;
            System.out.println(index);
//            List<LocationtableEntity>   locuslist   =   new ArrayList<LocationtableEntity>();
            for (int i = 0;i < index;i++) {

                String  onelocationString[] =   new String[2];
                onelocationString    =   locationStringArr[i].split(",");           //获取一个坐标
/*                System.out.println(onelocationString[0]);
                System.out.println(onelocationString[1]);*/
                LocationtableEntity locus = new LocationtableEntity();
                locus.setLongitude(Double.valueOf(onelocationString[1]));
                locus.setLatitude(Double.valueOf(onelocationString[0]));
                locus.setSporthistorytableBySpohisId(sporthis);
                locOperation.addOneLocation(locus);
            }





/*            List<SporthistorytableEntity>   sporthistorytableEntityList =   new ArrayList<SporthistorytableEntity>();
            sporthistorytableEntityList =   sportOperation.findSpoHis();
            for (int i = 0;i < sporthistorytableEntityList.size();i++)
            {
                System.out.println(sporthistorytableEntityList.get(i).getUsertableByUserId().getId());
                System.out.println(sporthis.getUsertableByUserId().getId());
                System.out.println(sporthistorytableEntityList.get(i).getStartTiem());

                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowTime = df.format(sporthis.getStartTiem());
                System.out.println(Timestamp.valueOf(nowTime));


                System.out.println(sporthis.getStartTiem().toString());
*//*                if(sporthistorytableEntityList.get(i).getUsertableByUserId().getId() == sporthis.getUsertableByUserId().getId()
                        &&  sporthistorytableEntityList.get(i).getStartTiem().equals(sporthis.getStartTiem()))
                {
                    System.out.println(sporthistorytableEntityList.get(i).toString());
                }*//*
            }*/

            /*sporthis    =   sportOperation.findSpoByStartTime(sporthis);
            System.out.println(sporthis.getId());
            System.out.println(sporthis.getEndTime());*/

/*            List<LocationtableEntity>   locuslist   =   new ArrayList<LocationtableEntity>();
            for (int i = 0;i < json_2_state_plus.getTrack().size();i++) {
                LocationtableEntity locus = new LocationtableEntity();
                locus.setLongitude(json_2_state_plus.getTrack().get(i).getLongitude());
                locus.setLatitude(json_2_state_plus.getTrack().get(i).getLatitude());
                locus.setSporthistorytableBySpohisId(sporthis);
                locOperation.addOneLocation(locus);
            }*/

            if (addResult){
                //添加成功
                System.out.println("添加成功");
                json_2_state_plus.setResult(SportClientSymbol.Sport.RIDE_RESULT_SUCCESS);
            }
            else {
                //添加失败
                System.out.println("添加失败");
                json_2_state_plus.setResult(SportClientSymbol.Sport.RIDE_RESULT_FAIL_DATABASEWRONG);
            }

        }

        return json_2_state_plus;
    }




}

